// pages/search/index.js
const $api = require("../../../api/book").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    from:1,
    loglists:[],
    hotlists:[],
    page:1,
    keywords:"",
    datalists:[],
    searched:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    t.setData({
      from:options.from
    })
    t.getMemberBooksKeywordsList()
    t.getBooksKeywordsList()

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var t = this
    if(t.data.searched){
      t.setData({
        page:t.data.page+1
      })
    }
  },
  // 获取个人检索记录
  getMemberBooksKeywordsList(){
    var t = this,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id
    }
    $api.getMemberBooksKeywordsList(data).then(res=>{
      console.log(res)
      t.setData({
        loglists:res.data
      })
    })
  },
  // 获取书籍检索次数最多的关键字
  getBooksKeywordsList(){
    var t = this
    $api.getBooksKeywordsList().then(res=>{
      console.log(res)
      t.setData({
        hotlists:res.data
      })
    })

  },
  // 根据关键字获取书籍列表
  getBooksListByKeywords(){
    var t = this,
    data = {
      keywords:t.data.keywords,
      pageNo:t.data.page,
      member_id:wx.getStorageSync('memberinfo').member_id,
    }
    $api.getBooksListByKeywords(data).then(res=>{
      var datalists = t.data.datalists
      datalists = datalists.concat(res.data)
      t.setData({
        datalists:datalists,
        searched:true,
      })
      console.log(res)
    })
  },
  search(e){
    var t = this
    t.setData({
      keywords:e.detail,
      datalists:[],
    })
    t.getBooksListByKeywords()
  },
  clearsearch(){
    console.log(1111)
    var t = this
    t.setData({
      keywords:"",
      datalists:[],
      searched:false,
      page:1,
    })
    t.getMemberBooksKeywordsList()
  },
  bsearch(e){
    var t = this
    t.setData({
      keywords:e.currentTarget.dataset.keywords,
      datalists:[],
    })
    t.getBooksListByKeywords()

  }
})